package injection;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 外部配置文件作为依赖来源 @Value 注入<br/>
 * {@link org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor}
 * @author jackcfan
 * @date 2021/9/24
 */
@Configuration
@PropertySource(value="classpath:application.properties",encoding = "utf8")
public class PropertiesInjectionTest {
    /**
     * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency}<br>
     * Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor)
     * */
    @Value("${user.id}")
    private String userId;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration class 配置类
        applicationContext.register(PropertiesInjectionTest.class);

        // 启动 Spring 上下文
        applicationContext.refresh();

        PropertiesInjectionTest bean = applicationContext.getBean(PropertiesInjectionTest.class);
        System.out.println(bean.userId);

        applicationContext.close();
    }
}
